<?php
/**
 * Created by PhpStorm.
 * User: zhixuedong
 * Date: 2023-05-29
 * Time: 14:51
 */

namespace app\api\controller;

use app\common\service\PublicService;
use app\project\controller\Config;
use think\Controller;
use think\Db;
use think\Exception;
use think\Session;

class Interfacea extends Controller
{
    public function iflogin(){
        $project_user = Session::get("project_user");
        if(empty($project_user)){
            $black['code'] = 1;
            $black['msg'] = "请重新登录";
            echo json_encode($black);die;
        }
        return $project_user;
    }

    //曝光台统计操作
    public function statisticsApi(){

        $common = new Common();
        if(!empty($_POST['uid'])){
            $project_user = $common->testusername($_POST['uid']);
        }else{
            $project_user = $this->iflogin();
        }

        try {
            $thisTime = date("Y-m-d");
            $where['library'] = 1;
            $where['sign'] = ['not in',[2,5]];
            $where['signTime'] = ['lt',$thisTime];
            $where['exposureAuto'] = 0;

            if(empty($project_user['expose'])){
                $where['duty_person'] = ['like',"%".$project_user['name']."%"];
            }

            $count1 = Db::name("project_pro")->where($where)->count("*");

            unset($where);
            $where['b.type_id'] = 343;
            $where['b.status'] = ['neq',1];
            $where['b.time'] = ['lt',$thisTime];
            $where['a.library'] = ['in',[2]];
            $where['a.plan_category'] = 2;
            $where['exposureAuto'] = 0;

            if(empty($project_user['expose'])){
                $where['a.duty_person'] = ['like',"%".$project_user['name']."%"];
            }
            $count2 = Db::name("project_pro")->alias("a")
                ->join("project_pro_advance_plan b","a.id = b.pro_id or a.pid = b.pro_id")
                ->where($where)->count("*");


            unset($where);
            $where['b.type_id'] = 344;
            $where['b.status'] = ['neq',1];
            $where['b.time'] = ['lt',$thisTime];
            $where['a.library'] = ['in',[3]];
            $where['a.plan_category'] = 2;
            $where['exposureAuto'] = 0;
            if(empty($project_user['expose'])){
                $where['a.duty_person'] = ['like',"%".$project_user['name']."%"];
            }
            $count3 = Db::name("project_pro")->alias("a")
                ->join("project_pro_advance_plan b","a.id = b.pro_id or a.pid = b.pro_id")
                ->where($where)->count("*");

            unset($where);
            $where['b.type_id'] = 350;
            $where['b.status'] = ['neq',1];
            $where['b.time'] = ['lt',$thisTime];
            $where['a.library'] = ['in',[3]];
            $where['a.plan_category'] = 2;
            $where['exposureAuto'] = 0;
            if(empty($project_user['expose'])){
                $where['a.duty_person'] = ['like',"%".$project_user['name']."%"];
            }
            $count4 = Db::name("project_pro")->alias("a")
                ->join("project_pro_advance_plan b","a.id = b.pro_id or a.pid = b.pro_id")
                ->where($where)->count("*");



            unset($where);
            $where['b.type_id'] = ['not in',[343,344,350]];
            $where['b.status'] = ['neq',1];
            $where['b.time'] = ['lt',$thisTime];
            $where['a.library'] = ['in',[2,3]];
            $where['a.plan_category'] = 2;
            $where['exposureAuto'] = 0;
            if(empty($project_user['expose'])){
                $where['a.duty_person'] = ['like',"%".$project_user['name']."%"];
            }
            $count5 = Db::name("project_pro")->alias("a")
                ->join("project_pro_advance_plan b","a.id = b.pro_id or a.pid = b.pro_id")
                ->where($where)->group("a.id")->count("*");

            $black['code'] = 0;
            $black['msg'] = "查询成功";
            $black['count1'] = $count1;
            $black['count2'] = $count2;
            $black['count3'] = $count3;
            $black['count4'] = $count4;
            $black['count5'] = $count5;
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);

    }

    //逾期未签约
    public function exposureApi(){
        $common = new Common();
        if(!empty($_POST['uid'])){
            $project_user = $common->testusername($_POST['uid']);
        }else{
            $project_user = $this->iflogin();
        }
        try {
            $page = empty($_POST['page'])?1:intval($_POST['page']);
            $limit = empty($_POST['limit'])?100:intval($_POST['limit']);
            $start = ($page - 1) * $limit;

            $type = empty($_POST['type'])?0:$_POST['type'];

            $list = [];
            $count = 0;
            switch ($type){
                case 1: //逾期未签约
                    $thisTime = date("Y-m-d");
                    $where['library'] = 1;
                    $where['sign'] = ['not in',[2,5]];
                    $where['signTime'] = ['lt',$thisTime];
                    $where['exposureAuto'] = 0;
                    if(empty($project_user['expose'])){
                        $where['duty_person'] = ['like',"%".$project_user['name']."%"];
                    }


                    if(!empty($_POST['keyword'])){
                        $where['title'] = ['like',"%".$_POST['keyword']."%"];
                    }


                    $list = Db::name("project_pro")->where($where)->limit($start,$limit)->select();
                    $signArray = [1=>"未签约",2=>"已签约",3=>"已逾期",4=>"暂缓推进",5=>"在谈项目"];
                    foreach ($list as $k=>$v){
                        $list[$k]['sinaName'] = empty($signArray[$v['sign']])?"":$signArray[$v['sign']];
                    }
                    $count = Db::name("project_pro")->where($where)->count("*");
                    break;
                case 2: //逾期未开工
                    $thisTime = date("Y-m-d");
                    $where['b.type_id'] = 343;
                    $where['b.status'] = ['neq',1];
                    $where['b.time'] = ['lt',$thisTime];
                    $where['a.library'] = ['in',[2]];
                    $where['a.plan_category'] = 2;
                    $where['exposureAuto'] = 0;
                    if(!empty($_POST['keyword'])){
                        $where['a.title'] = ['like',"%".$_POST['keyword']."%"];
                    }

                    if(empty($project_user['expose'])){
                        $where['a.duty_person'] = ['like',"%".$project_user['name']."%"];
                    }

                    $list = Db::name("project_pro")->alias("a")
                        ->join("project_pro_advance_plan b","a.id = b.pro_id or a.pid = b.pro_id")
                        ->field("a.*")->where($where)->limit($start,$limit)->select();
                    $signArray = [1=>"未签约",2=>"已签约",3=>"已逾期",4=>"暂缓推进",5=>"在谈项目"];
                    $start_status = [1=>"待开工",2=>"已开工",3=>"已逾期",4=>"已终止"];
                    $warehousing = [1=>"待入库",2=>"已入库"];
                    foreach ($list as $k=>$v){
                        $list[$k]['sinaName'] = empty($signArray[$v['sign']])?"":$signArray[$v['sign']];
                        $list[$k]['startName'] = empty($start_status[$v['start_status']])?"":$start_status[$v['start_status']];
                        $list[$k]['warehousingName'] = empty($warehousing[$v['warehousing']])?"":$warehousing[$v['warehousing']];
                    }
                    $count = Db::name("project_pro")->alias("a")
                        ->join("project_pro_advance_plan b","a.id = b.pro_id or a.pid = b.pro_id")
                        ->where($where)->count("*");
                    break;
                case 3: //逾期未入统
                    $thisTime = date("Y-m-d");
                    $where['b.type_id'] = 344;
                    $where['b.status'] = ['neq',1];
                    $where['b.time'] = ['lt',$thisTime];
                    $where['a.library'] = ['in',[3]];
                    $where['a.plan_category'] = 2;
                    $where['exposureAuto'] = 0;
                    if(!empty($_POST['keyword'])){
                        $where['a.title'] = ['like',"%".$_POST['keyword']."%"];
                    }

                    if(empty($project_user['expose'])){
                        $where['a.duty_person'] = ['like',"%".$project_user['name']."%"];
                    }

                    $list = Db::name("project_pro")->alias("a")
                        ->join("project_pro_advance_plan b","a.id = b.pro_id or a.pid = b.pro_id")
                        ->field("a.*")->where($where)->limit($start,$limit)->select();
                    $signArray = [1=>"未签约",2=>"已签约",3=>"已逾期",4=>"暂缓推进",5=>"在谈项目"];
                    $start_status = [1=>"待开工",2=>"已开工",3=>"已逾期",4=>"已终止"];
                    $warehousing = [1=>"待入库",2=>"已入库"];
                    foreach ($list as $k=>$v){
                        $list[$k]['sinaName'] = empty($signArray[$v['sign']])?"":$signArray[$v['sign']];
                        $list[$k]['startName'] = empty($start_status[$v['start_status']])?"":$start_status[$v['start_status']];
                        $list[$k]['warehousingName'] = empty($warehousing[$v['warehousing']])?"":$warehousing[$v['warehousing']];
                    }
                    $count = Db::name("project_pro")->alias("a")
                        ->join("project_pro_advance_plan b","a.id = b.pro_id or a.pid = b.pro_id")
                        ->where($where)->count("*");
                    break;
                case 4: //逾期未投产
                    $thisTime = date("Y-m-d");
                    $where['b.type_id'] = 350;
                    $where['b.status'] = ['neq',1];
                    $where['b.time'] = ['lt',$thisTime];
                    $where['a.library'] = ['in',[3]];
                    $where['a.plan_category'] = 2;
                    $where['exposureAuto'] = 0;
                    if(!empty($_POST['keyword'])){
                        $where['a.title'] = ['like',"%".$_POST['keyword']."%"];
                    }

                    if(empty($project_user['expose'])){
                        $where['a.duty_person'] = ['like',"%".$project_user['name']."%"];
                    }

                    $list = Db::name("project_pro")->alias("a")
                        ->join("project_pro_advance_plan b","a.id = b.pro_id or a.pid = b.pro_id")
                        ->field("a.*")->where($where)->limit($start,$limit)->select();
                    $signArray = [1=>"未签约",2=>"已签约",3=>"已逾期",4=>"暂缓推进",5=>"在谈项目"];
                    $start_status = [1=>"待开工",2=>"已开工",3=>"已逾期",4=>"已终止"];
                    $warehousing = [1=>"待入库",2=>"已入库"];
                    foreach ($list as $k=>$v){
                        $list[$k]['sinaName'] = empty($signArray[$v['sign']])?"":$signArray[$v['sign']];
                        $list[$k]['startName'] = empty($start_status[$v['start_status']])?"":$start_status[$v['start_status']];
                        $list[$k]['warehousingName'] = empty($warehousing[$v['warehousing']])?"":$warehousing[$v['warehousing']];
                    }
                    $count = Db::name("project_pro")->alias("a")
                        ->join("project_pro_advance_plan b","a.id = b.pro_id or a.pid = b.pro_id")
                        ->where($where)->count("*");
                    break;
                case 5: //其他节点逾期
                    $thisTime = date("Y-m-d");
                    $where['b.type_id'] = ['not in',[343,344,350]];
                    $where['b.status'] = ['neq',1];
                    $where['b.time'] = ['lt',$thisTime];
                    $where['a.library'] = ['in',[2,3]];
                    $where['a.plan_category'] = 2;
                    $where['exposureAuto'] = 0;
                    if(!empty($_POST['keyword'])){
                        $where['a.title'] = ['like',"%".$_POST['keyword']."%"];
                    }
                    if(empty($project_user['expose'])){
                        $where['a.duty_person'] = ['like',"%".$project_user['name']."%"];
                    }
                    $list = Db::name("project_pro")->alias("a")
                        ->join("project_pro_advance_plan b","a.id = b.pro_id or a.pid = b.pro_id")
                        ->field("a.*")->where($where)->limit($start,$limit)->group("a.id")->select();
                    $signArray = [1=>"未签约",2=>"已签约",3=>"已逾期",4=>"暂缓推进",5=>"在谈项目"];
                    $start_status = [1=>"待开工",2=>"已开工",3=>"已逾期",4=>"已终止"];
                    $warehousing = [1=>"待入库",2=>"已入库"];
                    foreach ($list as $k=>$v){
                        $list[$k]['sinaName'] = empty($signArray[$v['sign']])?"":$signArray[$v['sign']];
                        $list[$k]['startName'] = empty($start_status[$v['start_status']])?"":$start_status[$v['start_status']];
                        $list[$k]['warehousingName'] = empty($warehousing[$v['warehousing']])?"":$warehousing[$v['warehousing']];
                    }
                    $count = Db::name("project_pro")->alias("a")
                        ->join("project_pro_advance_plan b","a.id = b.pro_id or a.pid = b.pro_id")
                        ->where($where)->group("a.id")->count("*");
                    break;
            }

            $black['code'] = 0;
            $black['msg'] = "查询成功";
            $black['list'] = $list;
            $black['count'] = $count;
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }





    //整改完成情况提交
    public function overseerCreate(){
        $common = new Common();
        if(!empty($_POST['uid'])){
            $project_user = $common->testusername($_POST['uid']);
        }else{
            $project_user = $this->iflogin();
        }
        try {
            $proId = empty($_POST['pro_id'])?0:$_POST['pro_id'];
            $where['id'] = $proId;
            $thisDictionarie = $this->getDictionarie($project_user);
            $change_unit = PublicService::fieldService("project_pro",$where,"change_unit");
            $change_unit = explode(";",$change_unit);
            if(!in_array($thisDictionarie,$change_unit)){
                throw new Exception("你所在单位不属于整改单位,暂无提交权限");
            }
            $data = $_POST;
            $data['account_id'] = $project_user['id'];
            $data['addtime'] = time();
            PublicService::saveService("project_pro_overseer",$data,1);

            $black['code'] = 0;
            $black['msg'] = "操作成功";
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }

    //督办概况
    public function overseerApi(){
        $common = new Common();
        if(!empty($_POST['uid'])){
            $project_user = $common->testusername($_POST['uid']);
        }else{
            $project_user = $this->iflogin();
        }
        try {
            $proId = empty($_POST['pro_id'])?0:$_POST['pro_id'];
            $where['id'] = $proId;
            $overseerDetail = PublicService::detailService("project_pro",$where,1,"cause,overseer_course,leader,change_unit,change_time");
            $overseerDetail['leader'] = trim($overseerDetail['leader'],";");
            $overseerDetail['change_unit'] = trim($overseerDetail['change_unit'],";");

            unset($where);
            $where['pro_id'] = $proId;
            $list = PublicService::ListService("project_pro_overseer",$where);
            foreach ($list as $k=>$v){
                if(empty($v['images'])){
                    $list[$k]['images'] = [];
                }else{
                    $list[$k]['images'] = explode(";",$v['images']);
                }

                unset($accountWhere);
                if(!empty($v['add_id'])){
                    $accountWhere['id'] = $v['add_id'];
                    $sumbitName = Db::name("account")->where($accountWhere)->value("account");
                }else{
                    $accountWhere['id'] = $v['account_id'];
                    $sumbitName = Db::name("project_account")->where($accountWhere)->value("name");
                }
                $list[$k]['sumbitName'] = $sumbitName;
            }

            $black['list'] = $list;
            $black['overseerDetail'] = $overseerDetail;
            $black['code'] = 0;
            $black['msg'] = "查询成功";
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }


    //获取领导点评未回复的项目
    public function noReply(){
        $common = new Common();
        if(!empty($_POST['uid'])){
            $project_user = $common->testusername($_POST['uid']);
        }else{
            $project_user = $this->iflogin();
        }

        try {
            $page = empty($_POST['page'])?1:$_POST['page'];
            $limit = empty($_POST['limit'])?10:$_POST['limit'];
            $start = ($page-1)*$limit;

            if(!empty($_POST['keyword'])){
                $where['b.title'] = ["like","%{$_POST['keyword']}%"];
            }

            $where['a.pid'] = 0;
            $where['a.reply'] = 0;
            $where['a.content'] = ['neq',''];

            if(empty($project_user['expose'])){
                $where['b.duty_person'] = ['like',"%".$project_user['name']."%"];
            }


            $where['b.library'] = ['in',[1,2,3,6]];
            $list = Db::name("project_pro_comment")->alias("a")
                ->join("project_pro b","a.pro_id = b.id")->group("b.id")
                ->field("b.*")->where($where)->limit($start,$limit)->select();



            $count = Db::name("project_pro_comment")->alias("a")
                ->join("project_pro b","a.pro_id = b.id")->group("b.id")
                ->field("b.*")->where($where)->count("*");
            $library = \config("library");
            foreach ($list as $k=>$v){
                $list[$k]['flag'] = empty($library[$v['library']])?"":$library[$v['library']];
            }
            $black['code'] = 0;
            $black['msg'] = "查询成功";
            $black['list'] = $list;
            $black['count'] = $count;
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);

    }


    //其他逾期办理事项统计
    public function helpStatisticsApi(){
        $common = new Common();
        if(!empty($_POST['uid'])){
            $project_user = $common->testusername($_POST['uid']);
        }else{
            $project_user = $this->iflogin();
        }

        try {
            $type = [0,1];
            foreach ($type as $k=>$item){
                $where = "pid = 0 and status=6 and type = ".$item;
                if(empty($project_user['expose'])){
                    $where .="  and (sumbit_id = ".$project_user['id']." or accountId like '%,".$project_user['id'].",%')";
                }
                $type[$k] = Db::name("project_help")->where($where)->count("*");
            }

            unset($where);
            $where['a.pid'] = 0;
            $where['a.reply'] = 0;
            $where['a.content'] = ['neq',''];
            if(empty($project_user['expose'])){
                $where['b.duty_person'] = ['like',"%".$project_user['name']."%"];
            }
            $where['b.library'] = ['in',[1,2,3,6]];
            $count = Db::name("project_pro_comment")->alias("a")
                ->join("project_pro b","a.pro_id = b.id")->group("b.id")
                ->field("b.*")->where($where)->count("*");
            $type[] = $count;

            $count6 = 0;
            foreach ($type as $item){
                $count6 +=$item;
            }

            $black['code'] = 0;
            $black['msg'] = "查询成功";
            $black['type'] = $type;
            $black['count6'] = $count6;
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);

    }

    //获取服务直通车未回复事项
    public function getHelp(){
        $common = new Common();
        if(!empty($_POST['uid'])){
            $project_user = $common->testusername($_POST['uid']);
        }else{
            $project_user = $this->iflogin();
        }
        try {
            $page = empty($_POST['page'])?1:$_POST['page'];
            $limit = empty($_POST['limit'])?10:$_POST['limit'];
            $start = ($page-1)*$limit;

            $type = empty($_POST['type'])?0:$_POST['type'];
            $where = "pid = 0 and status=6 and type = ".$type;

            if(empty($project_user['expose'])){
                $where .="  and (sumbit_id = ".$project_user['id']." or accountId like '%,".$project_user['id'].",%')";
            }

            if(!empty($_POST['keyword'])){
                $proWhere['title'] = ["like","%{$_POST['keyword']}%"];
                $proList = Db::name("project_pro")->where($proWhere)->field("id")->select();
                $proList = array_column($proList,"id");
                $proList = implode(",",$proList);
                $where .=" and pro_id in(".$proList.")";
            }


            $list = Db::name("project_help")->where($where)->limit($start,$limit)->order("id desc")->select();
            $count = Db::name("project_help")->where($where)->count("*");

            foreach ($list as $k=>$v){
                //查询项目名称
                $proWhere['id'] = $v['pro_id'];
                $proName = Db::name("project_pro")->where($proWhere)->value("title");
                $list[$k]['proName'] = empty($proName)?"":$proName;
                $list[$k]['addtime'] = date("Y-m-d H:i:s",$v['addtime']);

                //查询提交人
                $sumbitName = '';
                $accountWhere['id'] = $v['sumbit_id'];
                $sumbitName = Db::name("project_account")->where($accountWhere)->value("name");
                $list[$k]['sumbitName'] = $sumbitName;

                //查询协调人员信息
                $accountId = explode(",",$v['accountId']);
                $accountId = array_filter($accountId);
                $accountWhere1['id'] = ['in',$accountId];
                $coordinateList = $common->public_select("project_account",$accountWhere1,"id,name,dictionariesId")->select();
                $list[$k]['coordinate'] = $coordinateList;
            }

            $black['code'] = 0;
            $black['msg'] = "查询成功";
            $black['list'] = $list;
            $black['count'] = $count;
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }


    //获取应税收入
    public function revenueApi(){
        $common = new Common();
        if(!empty($_POST['uid'])){
            $project_user = $common->testusername($_POST['uid']);
        }else{
            $project_user = $this->iflogin();
        }
        try {
            $where['pro_id'] = $_POST['pro_id'];
            $revenue = PublicService::detailService("project_pro_tax_revenue",$where);
            $black['code'] = 0;
            $black['msg'] = "查询成功";
            $black['revenue'] = $revenue;
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }

    //更新应税收入
    public function revenueSave(){
        $common = new Common();
        if(!empty($_POST['uid'])){
            $project_user = $common->testusername($_POST['uid']);
        }else{
            $project_user = $this->iflogin();
        }
        try {
            $pro_id = $_POST['pro_id'];
            $duty_person = Db::name("project_pro")->where(['id'=>$pro_id])->value("duty_person");
            $duty_person = explode(";",$duty_person);
            if(!in_array($project_user['name'],$duty_person)){
                throw new Exception("您不是该项目的负责人，不能进行更新操作");
            }

            $revenue = $_POST;
            unset($revenue['id']);

            $revenue['year'] = empty($revenue['year'])?date("Y"):$revenue['year'];
            $revenue['pro_id'] = $pro_id;

            $where['pro_id'] = $pro_id;
            $detail = Db::name("project_pro_tax_revenue")->where($where)->find();
            if(empty($detail)){
                Db::name("project_pro_tax_revenue")->insert($revenue);
            }else{
                Db::name("project_pro_tax_revenue")->where($where)->update($revenue);
            }

            $black['code'] = 0;
            $black['msg'] = "操作成功";
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }


    //曝光台所有应曝光项目
    public function exposureAll(){
        $common = new Common();
        if(!empty($_POST['uid'])){
            $project_user = $common->testusername($_POST['uid']);
        }else{
            $project_user = $this->iflogin();
        }
        try {
            $thisTime = date("Y-m-d");
            $thisTime = date("Y-m-d");
            $where['library'] = 1;
            $where['sign'] = ['not in',[2,5]];
            $where['signTime'] = ['lt',$thisTime];
            $where['exposureAuto'] = 0;
            if(empty($project_user['expose'])){
                $where['duty_person'] = ['like',"%".$project_user['name']."%"];
            }
            $list1 = Db::name("project_pro")->where($where)->field("id,title,library")->select();

            unset($where);
            $where['b.type_id'] = 343;
            $where['b.status'] = ['neq',1];
            $where['b.time'] = ['lt',$thisTime];
            $where['a.library'] = ['in',[3]];
            $where['a.plan_category'] = 2;
            $where['exposureAuto'] = 0;
            if(empty($project_user['expose'])){
                $where['a.duty_person'] = ['like',"%".$project_user['name']."%"];
            }
            $list2 = Db::name("project_pro")->alias("a")
                ->join("project_pro_advance_plan b","a.id = b.pro_id or a.pid = b.pro_id")
                ->where($where)->field("a.id,a.title,a.library")->select();

            unset($where);
            $where['b.type_id'] = 344;
            $where['b.status'] = ['neq',1];
            $where['b.time'] = ['lt',$thisTime];
            $where['a.library'] = ['in',[3]];
            $where['a.plan_category'] = 2;
            $where['exposureAuto'] = 0;
            if(empty($project_user['expose'])){
                $where['a.duty_person'] = ['like',"%".$project_user['name']."%"];
            }
            $list3 = Db::name("project_pro")->alias("a")
                ->join("project_pro_advance_plan b","a.id = b.pro_id or a.pid = b.pro_id")
                ->where($where)->field("a.id,a.title,a.library")->select();

            unset($where);
            $where['b.type_id'] = 350;
            $where['b.status'] = ['neq',1];
            $where['b.time'] = ['lt',$thisTime];
            $where['a.library'] = ['in',[3]];
            $where['a.plan_category'] = 2;
            $where['exposureAuto'] = 0;
            if(empty($project_user['expose'])){
                $where['a.duty_person'] = ['like',"%".$project_user['name']."%"];
            }
            $list4 = Db::name("project_pro")->alias("a")
                ->join("project_pro_advance_plan b","a.id = b.pro_id or a.pid = b.pro_id")
                ->where($where)->field("a.id,a.title,a.library")->select();

            unset($where);
            $where['b.type_id'] = ['not in',[343,344,350]];
            $where['b.status'] = ['neq',1];
            $where['b.time'] = ['lt',$thisTime];
            $where['a.library'] = ['in',[2,3]];
            $where['a.plan_category'] = 2;
            $where['exposureAuto'] = 0;
            if(!empty($_POST['keyword'])){
                $where['a.title'] = ['like',"%".$_POST['keyword']."%"];
            }
            if(empty($project_user['expose'])){
                $where['a.duty_person'] = ['like',"%".$project_user['name']."%"];
            }
            $list5 = Db::name("project_pro")->alias("a")
                ->join("project_pro_advance_plan b","a.id = b.pro_id or a.pid = b.pro_id")
                ->where($where)->field("a.id,a.title,a.library")->select();

            $list = array_merge($list1,$list2,$list3,$list4,$list5);
            $list = array_column($list,null,"id");
            $black['code'] = 0;
            $black['msg'] = "查询成功";
            $black['list'] = $list;
            $black['count'] = count($list);
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }


    //我的项目
    public function myList(){
        $common = new Common();
        if(!empty($_POST['uid'])){
            $project_user = $common->testusername($_POST['uid']);
        }else{
            $project_user = $this->iflogin();
        }
        try {
            $page = empty($_POST['page'])?1:intval($_POST['page']);
            $limit = empty($_POST['limit'])?100:intval($_POST['limit']);
            $start = ($page - 1) * $limit;
            $menuId = empty($_POST['menuId'])?0:$_POST['menuId'];

            //查询我的县级领导项目
            $name = $project_user['name'];


            if($menuId==4){
                $thisDictionarie = $this->getDictionarie($project_user);
                //获取服务单位
                $planWhere['duty_unit'] = ['like',"%$thisDictionarie%"];
                $planList = Db::name("project_pro_advance_plan")->where($planWhere)->group("pro_id")->select();
                $where = "1=1 and parent_id = 0";
                $whereOr = "leader like '%".$name."%' or duty_unit like '%".$thisDictionarie."%'";
                if(!empty($planList)){
                    $planList = array_column($planList,'pro_id');
                    $planList = implode($planList,",");
                    $whereOr .=" or ((id in (".$planList.") or pid in (".$planList.")) and library in(2,3,4)) ";
                }
                $where .= " and ($whereOr)";

                if(!empty($_POST['keyword'])){
                    $where .=" and title like '%".$_POST['keyword']."%'";
                }
                $list = Db::name("project_pro")->where($where)->limit($start,$limit)->select();
                $count = Db::name("project_pro")->where($where)->count("*");
            }else{
                switch ($menuId){
                    case 1:
                        $where = "1=1 and parent_id = 0 and library in(1,2,3";
                        $where .= " and leader like '%".$name."%'";
                        break;
                    case 2:
                        $where = "1=1 and parent_id = 0 and library in(1,2,3)";
                        $thisDictionarie = $this->getDictionarie($project_user);
                        $where .= " and duty_unit like '%".$thisDictionarie."%'";
                        break;
                    case 3:
                        $thisDictionarie = $this->getDictionarie($project_user);
                        break;
                }

                if($menuId==3){
                    $where['a.library'] = ['in',[2,3]];
                    $where["b.duty_unit"] = ['like',"%$thisDictionarie%"];
                    $where['a.parent_id'] = 0;
                    if(!empty($_POST['keyword'])){
                        $where['a.title'] = ['like',"%".$_POST['keyword']."%"];
                    }
                    $list = Db::name("project_pro")->alias("a")->join("project_pro_advance_plan b","a.id = b.pro_id or a.pid = b.pro_id")
                        ->where($where)->group("b.pro_id")
                        ->field("a.*")->limit($start,$limit)->select();
                    $count = Db::name("project_pro")->alias("a")->join("project_pro_advance_plan b","a.id = b.pro_id or a.pid = b.pro_id")
                        ->where($where)->group("b.pro_id")->count("*");
                }else{
                    if(!empty($_POST['keyword'])){
                        $where .=" and title like '%".$_POST['keyword']."%'";
                    }
                    $where .=" and sign!=2 and start_status!=2";
                    $list = Db::name("project_pro")->where($where)->limit($start,$limit)->select();
                    $count = Db::name("project_pro")->where($where)->count("*");
                }
            }




            $library = \config("library");
            foreach ($list as $k=>$v){
                $list[$k]['flag'] = empty($library[$v['library']])?"":$library[$v['library']];
                $list[$k]['adopt'] = empty($v['adopt'])?0:$v['adopt'];
                if(strpos($v['duty_person'],$project_user['name'])!==false){
                    //是否存在点评信息
                    $commentCount = Db::name("project_pro_comment")->where(['pro_id'=>$v['id']])->count("id");
                    //已读数
                    $readCount = Db::name("project_pro_comment_log")->where(['pro_id'=>$v['id'],'read_id'=>$project_user['id']])->count("id");
                    //是否存在未读信息
                    if($commentCount!=0){
                        $list[$k]['read'] = ($commentCount!=$readCount)?1:2;
                    }
                    $list[$k]['leading_cadre'] = 1;
                }

            }
            $black['code'] = 0;
            $black['msg'] = "查询成功";
            $black['list'] = $list;
            $black['count'] = $count;
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }

    //获取我服务的企业
    public function enterprise(){
        $common = new Common();
        if(!empty($_POST['uid'])){
            $project_user = $common->testusername($_POST['uid']);
        }else{
            $project_user = $this->iflogin();
        }

        try {
            $thisDictionarie = $this->getDictionarie($project_user);
            $page = empty($_POST['page'])?1:intval($_POST['page']);
            $limit = empty($_POST['limit'])?100:intval($_POST['limit']);
            $where['library'] = 6;
//            $where['duty_unit'] = ['like',"%$thisDictionarie%"];
            $where['duty_person'] = ['like',"%".$project_user['name']."%"];
            if(!empty($_POST['keyword'])){
                $where['title'] = ['like',"%".$_POST['keyword']."%"];
            }

            $returnBlack = PublicService::ListService("project_pro",$where,null,$page,$limit);
            $count = $returnBlack['count'];
            $list = $returnBlack['list'];

            $black['code'] = 0;
            $black['msg'] = "查询成功";
            $black['list'] = $list;
            $black['count'] = $count;
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }

    //待办事项
    public function matterApi(){
        $common = new Common();
        if(!empty($_POST['uid'])){
            $project_user = $common->testusername($_POST['uid']);
        }else{
            $project_user = $this->iflogin();
        }

        try {
            $page = empty($_POST['page'])?1:intval($_POST['page']);
            $limit = empty($_POST['limit'])?100:intval($_POST['limit']);
            $start = ($page - 1) * $limit;
            $type = empty($_POST['type'])?1:$_POST['type'];


            $where = "1=1";
            switch ($type){
                case 1:  //计划节点
                    $startTime = date("Y-m-d");
                    $endTime = date("Y-m-d",strtotime("+5 day"));
                    //计划今天完成的项目
                    $planWhere['a.time'][] = ['egt',$startTime];
                    $planWhere['a.time'][] = ['elt',$endTime];
                    $planWhere['a.status'] = ['neq',1];
                    $planWhere['b.library'] = ['in',[2,3]];
                    $planWhere['b.duty_person'] = ['like',"%".$project_user['name']."%"];
                    $planList = Db::name("project_pro_advance_plan")->alias("a")
                        ->join("project_pro b","a.pro_id = b.id or a.pro_id = b.pid")
                        ->where($planWhere)->field("b.id")->group("pro_id")->select();
                    $planList = array_column($planList,"id");
                    //计划今天完成的项目end

                    if(empty($planList)){
                        $planList = -1;
                    }else{
                        $planList = implode(",",$planList);
                    }

                    $where .=" and library not in(4,5,6) and id in(".$planList.")";
                break;
                case 2:  //项目进展
                    $where .= " and library not in(4,5,6) and duty_person like '%".$project_user['name']."%'";
                    $myPro = Db::name("project_pro")->where($where)->field("id")->select();
                    $myPro = array_column($myPro,"id");
                    //本月已汇报的项目
                    $beginTime=mktime(0,0,0,date("m"),01,date("Y"));
                    $endTime=mktime(23,59,59,date("m"),date("t"),date("Y"));
                    $progressWhere['pid'] = ['in',$myPro];
                    $progressWhere['addtime'][] = ['gt',$beginTime];
                    $progressWhere['addtime'][] = ['elt',$endTime];
                    $progressId = Db::name("project_progress")->where($progressWhere)->field("pid")->group("pid")->select();
                    $progressId = array_column($progressId,"pid");
                    foreach($myPro as $k=>$v){
                        if(in_array($v,$progressId)){
                            unset($myPro[$k]);
                        }
                    }
                    //本月未汇报数据
                    if(empty($myPro)){
                        $myPro = -1;
                    }else{
                        $myPro = implode(",",$myPro);
                    }
                    $where .=" and id in(".$myPro.")";
                break;
                case 3: //企业走访
                    $where .= " and library=6 and menuTop=2 and duty_person like '%".$project_user['name']."%'";
                    $myPro = Db::name("project_pro")->where($where)->field("id")->select();
                    $myPro = array_column($myPro,"id");
                    //本月已走访企业
                    $progressWhere['pro_id'] = ['in',$myPro];
                    $progressWhere['addtime'] = ['like',"%".date("Y-m")."%"];
                    $progressId = Db::name("project_pro_figure")->where($progressWhere)->field("pro_id")->group("pro_id")->select();
                    $progressId = array_column($progressId,"pro_id");
                    foreach($myPro as $k=>$v){
                        if(in_array($v,$progressId)){
                            unset($myPro[$k]);
                        }
                    }
                    //本月未汇报数据
                    if(empty($myPro)){
                        $myPro = -1;
                    }else{
                        $myPro = implode(",",$myPro);
                    }
                    $where .=" and id in(".$myPro.")";
                break;
            }


            if(!empty($_POST['keyword'])){
                $where .=" and title like '%".$_POST['keyword']."%'";
            }
            
            $list = Db::name("project_pro")->where($where)->limit($start,$limit)->select();
            $count = Db::name("project_pro")->where($where)->count("*");

            $library = \config("library");
            foreach ($list as $k=>$v){
                $list[$k]['flag'] = empty($library[$v['library']])?"":$library[$v['library']];
            }

            $black['code'] = 0;
            $black['msg'] = "查询成功";
            $black['list'] = $list;
            $black['count'] = $count;
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }



    //获取用户责任单位
    public function getDictionarie($project_user){
        //获取用户责任单位
        $accountWhere['id'] = $project_user['id'];
        $dictionariesId = Db::name("project_account")->where($accountWhere)->value("dictionariesId");
        $dictionariesWhere['id'] = $dictionariesId;
        $thisDictionarie = Db::name("project_dictionaries")->where($dictionariesWhere)->value("name");
        return $thisDictionarie;
    }


    //搜索项目
    public function listApi(){
        $common = new Common();
        if(!empty($_POST['uid'])){
            $project_user = $common->testusername($_POST['uid']);
        }else{
            $project_user = $this->iflogin();
        }
        try {
            $name = $project_user['name'];

            $page = empty($_POST['page'])?1:intval($_POST['page']);
            $limit = empty($_POST['limit'])?100:intval($_POST['limit']);
//            $limit = 100;
            $start = ($page - 1) * $limit;

            //已关联项目
            $proWhere['account_id'] = $project_user['id'];
            $project_id = Db::name("project_account_pro")->where($proWhere)->field("project_id")->select();
            if(!empty($project_id)){
                $project_id = array_column($project_id,'project_id');
                $project_ids = implode($project_id,",");
            }
            if(empty($project_ids)){
                $project_ids = 0;
            }
            //已关联项目end

            $where = "1=1";
            if(!empty($_POST['keyword'])){
                $where .=" and (title like '%".$_POST['keyword']."%' or duty_unit like '%".$_POST['keyword']."%' or duty_person like '%".$_POST['keyword']."%') ";
            }

            if(!empty($project_ids)){
                $where .= " and (id in (".$project_ids.") or duty_person like '%".$name."%' or leader like '%".$name."%') and status=1";
            }


            $where .=" and start_status!=2 and sign!=2 and library in(1,2,3,6)";


            $order = "sort desc,id desc";
            $list = Db::name("project_pro")->where($where)
                ->field("id,title,thumb,duty_person,leader,progress,year_progress,duty_unit,project_status,difficulty,sign,start_status,warehousing,reply,liaison,operation,signTime,warehousingtitle,plan_onstream_time,live_url,library,deviceSerial,possibility,adopt")
                ->limit($start,$limit)
                ->order($order)->select();

            $count = Db::name("project_pro")->where($where)->count("*");
            $black['code'] = 0;
            $black['msg'] = "查询成功";
            $black['list'] = $list;
            $black['count'] = $count;
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }



    public function helpList(){
        $common = new Common();
        $nowtime = time();
        if(!empty($_POST['uid'])){
            $project_user = $common->testusername($_POST['uid']);
        }else{
            $project_user = $this->iflogin();
        }
        try{
            $page = empty($_POST['page'])?1:$_POST['page'];
            $limit = empty($_POST['limit'])?10:$_POST['limit'];
            $start = ($page-1)*$limit;

            $readCount = 0;

            //获取已读信息
            $logWhere['read_id'] = $project_user['id'];
            $logId = Db::name("project_help_log")->where($logWhere)->field("help_id")->select();  //已读消息
            $logId = array_column($logId,"help_id");

            $where['pid'] = 0;
            $type = empty($_POST['type'])?1:$_POST['type'];


            if(!empty($_POST['keyword'])){
                $proWhere['title'] = ["like","%{$_POST['keyword']}%"];
                $proList = Db::name("project_pro")->where($proWhere)->field("id")->select();
                $proList = array_column($proList,"id");
                $where['pro_id'] = ['in',$proList];
            }

            $where['status'] = ['in',[1,2,6]];
            $where['accountId'] = ['like',"%,{$project_user['id']},%"];
            //获取未读信息
            $readWhere['id'] = ['not in',$logId];
            $readWhere['accountId'] = ['like',"%,{$project_user['id']},%"];
            $readWhere['sumbit_id'] = ['neq',$project_user['id']];
            $readList = Db::name("project_help")->where($readWhere)->field("id,pid")->select();  //未读信息
            $readCount = count($readList);
            //获取未读信息end
            $readId1 = array_unique(array_column($readList,'id'));
            $readId2 = array_unique(array_column($readList,'pid'));
            $readId = array_unique(array_merge($readId1,$readId2));

            $list = Db::name("project_help")->where($where)->limit($start,$limit)->order("id desc")->select();
            $count = Db::name("project_help")->where($where)->count("*");

            $evaluate = [1=>"满意",2=>"不满意"];


            foreach ($list as $k=>$v){
                $list[$k]['evaluate'] = empty($evaluate[$v['evaluate']])?"":$evaluate[$v['evaluate']];

                if(in_array($v['id'],$readId)){
                    $readStatus = 1;
                }else{
                    $readStatus = 0;
                }
                $list[$k]['readStatus'] = $readStatus;
                $readWhere['pid'] = $v['id'];
                $Unread = Db::name("project_help")->where($readWhere)->count("*");
                if($Unread==0 && $readStatus==1){
                    $Unread++;
                }
                $list[$k]['Unread'] = $Unread;

                //查询项目名称
                $proWhere['id'] = $v['pro_id'];
                $proName = Db::name("project_pro")->where($proWhere)->value("title");
                $list[$k]['proName'] = empty($proName)?"":$proName;

                //查询提交人
                $sumbitName = '';
                $accountWhere['id'] = $v['sumbit_id'];
                $sumbitName = Db::name("project_account")->where($accountWhere)->value("name");
                $list[$k]['sumbitName'] = $sumbitName;

                //查询协调人员信息
                $accountId = explode(",",$v['accountId']);
                $accountId = array_filter($accountId);
                $accountWhere1['id'] = ['in',$accountId];
                $coordinateList = $common->public_select("project_account",$accountWhere1,"id,name,dictionariesId")->select();
                $list[$k]['coordinate'] = $coordinateList;
                $list[$k]['addtime'] = date("Y-m-d H:i:s",$v['addtime']);

            }
            $black['code'] = 0;
            $black['list'] = $list;
            $black['count'] = $count;
            $black['readCount'] = $readCount;
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }


    //待办事项统计
    public function withstatistics(){
        $common = new Common();
        if(!empty($_POST['uid'])){
            $project_user = $common->testusername($_POST['uid']);
        }else{
            $project_user = $this->iflogin();
        }

        try {



            $type = [1,2,3];
            foreach ($type as $key=>$val){
                $where = "1=1";
                unset($progressWhere);
                switch ($val){
                    case 1:
                        $startTime = date("Y-m-d");
                        $endTime = date("Y-m-d",strtotime("+5 day"));
                        //计划今天完成的项目
                        $planWhere['a.time'][] = ['egt',$startTime];
                        $planWhere['a.time'][] = ['elt',$endTime];
                        $planWhere['a.status'] = ['neq',1];
                        $planWhere['b.library'] = ['in',[2,3]];
                        $planWhere['b.duty_person'] = ['like',"%".$project_user['name']."%"];
                        $planList = Db::name("project_pro_advance_plan")->alias("a")
                            ->join("project_pro b","a.pro_id = b.id or a.pro_id = b.pid")
                            ->where($planWhere)->field("b.id")->group("pro_id")->select();
                        $planList = array_column($planList,"id");
                        //计划今天完成的项目end
                        if(empty($planList)){
                            $planList = -1;
                        }else{
                            $planList = implode(",",$planList);
                        }
                        $where .=" and library not in(4,5,6) and id in(".$planList.")";
                    break;
                    case 2:  //项目进展
                        $where .= " and library not in(4,5,6) and duty_person like '%".$project_user['name']."%'";
                        $myPro = Db::name("project_pro")->where($where)->field("id")->select();
                        $myPro = array_column($myPro,"id");
                        //本月已汇报的项目
                        $beginTime=mktime(0,0,0,date("m"),01,date("Y"));
                        $endTime=mktime(23,59,59,date("m"),date("t"),date("Y"));
                        $progressWhere['pid'] = ['in',$myPro];
                        $progressWhere['addtime'][] = ['gt',$beginTime];
                        $progressWhere['addtime'][] = ['elt',$endTime];
                        $progressId = Db::name("project_progress")->where($progressWhere)->field("pid")->group("pid")->select();
                        $progressId = array_column($progressId,"pid");
                        foreach($myPro as $k=>$v){
                            if(in_array($v,$progressId)){
                                unset($myPro[$k]);
                            }
                        }
                        //本月未汇报数据
                        if(empty($myPro)){
                            $myPro = -1;
                        }else{
                            $myPro = implode(",",$myPro);
                        }
                        $where .=" and id in(".$myPro.")";
                        break;
                    case 3: //企业走访
                        $where .= " and library=6 and menuTop=2 and duty_person like '%".$project_user['name']."%'";
                        $myPro = Db::name("project_pro")->where($where)->field("id")->select();
                        $myPro = array_column($myPro,"id");
                        //本月已走访企业
                        $progressWhere['pro_id'] = ['in',$myPro];
                        $progressWhere['addtime'] = ['like',"%".date("Y-m")."%"];
                        $progressId = Db::name("project_pro_figure")->where($progressWhere)->field("pro_id")->group("pro_id")->select();
                        $progressId = array_column($progressId,"pro_id");
                        foreach($myPro as $k=>$v){
                            if(in_array($v,$progressId)){
                                unset($myPro[$k]);
                            }
                        }
                        //本月未汇报数据
                        if(empty($myPro)){
                            $myPro = -1;
                        }else{
                            $myPro = implode(",",$myPro);
                        }
                        $where .=" and id in(".$myPro.")";
                        break;
                }
                $count = Db::name("project_pro")->where($where)->count("*");
                $type[$key] = $count;
            }





            unset($where);
            $where['pid'] = 0;
            $where['status'] = ['in',[1,2,6]];
            $where['accountId'] = ['like',"%,{$project_user['id']},%"];
            $type[] = Db::name("project_help")->where($where)->count("*");

            $black['code'] = 0;
            $black['msg'] = "查询成功";
            $black['type'] = $type;
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['line'] = $e->getLine();
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }



}